package com.xnyzc.lhy.order.service.impl.approval;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xnyzc.lhy.common.exception.PangException;
import com.xnyzc.lhy.common.penum.EErrorCode;
import com.xnyzc.lhy.common.penum.EOrderType;
import com.xnyzc.lhy.common.penum.approval.EApplyAmountType;
import com.xnyzc.lhy.common.penum.approval.EApprovalMatter;
import com.xnyzc.lhy.common.penum.approval.EApprovalOrderStatus;
import com.xnyzc.lhy.common.penum.approval.EApprovalTaskStatus;
import com.xnyzc.lhy.common.util.CheckUtil;
import com.xnyzc.lhy.common.util.IDUtil;
import com.xnyzc.lhy.order.entity.approval.OaCRaiseApply;
import com.xnyzc.lhy.order.entity.sidebar.OaCUserOffice;
import com.xnyzc.lhy.order.entity.util.OrderUtil;
import com.xnyzc.lhy.order.mapper.approval.OaCRaiseApplyMapper;
import com.xnyzc.lhy.order.mapper.sidebar.OaCUserOfficeMapper;
import com.xnyzc.lhy.order.service.approval.IOaCRaiseApplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

/**
 * <p>
 * 用户公务提额申请 服务实现类
 * </p>
 *
 * @author zhuyubin
 * @since 2019-08-09
 */
@Service
public class OaCRaiseApplyServiceImpl extends ServiceImpl<OaCRaiseApplyMapper, OaCRaiseApply> implements IOaCRaiseApplyService {

    /**
     * 审批首个节点
     */
    private static final Integer APPROVAL_NODE_FAST = 1;

    @Autowired
    private OaCRaiseApplyMapper oaCRaiseApplyMapper;
    @Autowired
    private OaCUserOfficeMapper oaCUserOfficeMapper;
    /**
     * 申请额度
     * @return
     */
    @Override
    public String applyAmount(Long userId, String tag, Integer type, BigDecimal applyAmount, String applyReason){
        // 查询重复提交
        Integer integer = oaCRaiseApplyMapper.selectCount(
                Wrappers.<OaCRaiseApply>lambdaQuery()
                        .eq(OaCRaiseApply::getUserId, userId)
                        .eq(OaCRaiseApply::getTag, tag)
                        .eq(OaCRaiseApply::getApplyAmountType, type)
                        .in(OaCRaiseApply::getApprovalStatus,
                                EApprovalOrderStatus.pending.getValue(),
                                EApprovalOrderStatus.approvaling.getValue()
                        )
        );
        if (integer > 0){
            throw PangException.create(EErrorCode.repeatedSubmit);
        }
        OaCUserOffice oaCUserOffice = oaCUserOfficeMapper.selectOne(
                Wrappers.<OaCUserOffice>lambdaQuery()
                        .eq(OaCUserOffice::getUserId, userId)
                        .eq(OaCUserOffice::getTag, tag)
        );
        if (CheckUtil.objIsEmpty(oaCUserOffice)){
            throw PangException.create(EErrorCode.unOffice);
        }
        // 部门id
        Long officeId = oaCUserOffice.getOfficeId();
        Integer applyMatter;
        Long approvalId;
        // 个人额度提额申请
        if (EApplyAmountType.personalOfficial.getValue().equals(type)){
            applyMatter = EApprovalMatter.personalAmountApproval.getValue();
            approvalId = EApprovalMatter.personalAmountApproval.getApprovalId();
        }
        // 部门公务提额申请
        else if (EApplyAmountType.deptOfficial.getValue().equals(type)){
            applyMatter = EApprovalMatter.officeAmountApproval.getValue();
            approvalId = EApprovalMatter.officeAmountApproval.getApprovalId();
        }
        // 非法状态
        else {
            throw PangException.create(EErrorCode.unApprovalType);
        }
        Date date = new Date();
        // 插入额度申请表
        this.insertOaCRaiseApply(userId, tag, officeId, applyAmount, applyReason, applyMatter, approvalId, date, type);
        return EErrorCode.applyAmountSuccess.getDesc();
    }

    /**
     * 插入用户公务提额申请
     * @param userId 用户id
     * @param officeId 部门id
     * @param applyAmount 申请额度
     * @param applyReason 申请原因
     * @param applyMatter 申请事项(参见字典)
     * @param approvalId 审批流模板id
     * @param tag 租户
     * @return
     */
    private void insertOaCRaiseApply(
            Long userId,
            String tag,
            Long officeId,
            BigDecimal applyAmount,
            String applyReason,
            Integer applyMatter,
            Long approvalId,
            Date date,
            Integer applyType
        ){
        OaCRaiseApply oaCRaiseApply = new OaCRaiseApply();
        oaCRaiseApply.setRaiseId(IDUtil.nextId());

        oaCRaiseApply.setUserId(userId);
        oaCRaiseApply.setOfficeId(officeId);
        oaCRaiseApply.setApplyAmount(applyAmount);
        // 个人
        if (EApplyAmountType.personalOfficial.getValue().equals(applyType)){
            oaCRaiseApply.setApplyAmountType(applyType);
            oaCRaiseApply.setOrderNo(OrderUtil.orderNo(EOrderType.TEGR));
        }
        // 部门
        else {
            oaCRaiseApply.setApplyAmountType(applyType);
            oaCRaiseApply.setOrderNo(OrderUtil.orderNo(EOrderType.TEGW));
        }
        oaCRaiseApply.setApplyReason(applyReason);
        oaCRaiseApply.setApplyMatter(applyMatter);
        oaCRaiseApply.setApprovalStatus(EApprovalTaskStatus.pending.getValue());
        oaCRaiseApply.setApprovalId(approvalId);
        oaCRaiseApply.setApprovalNode(APPROVAL_NODE_FAST);
        oaCRaiseApply.setTag(tag);
        oaCRaiseApply.setCreateUser(userId);
        oaCRaiseApply.setCreateTime(date);
        oaCRaiseApply.setUpdateUser(userId);
        oaCRaiseApply.setUpdateTime(date);
        int insert = oaCRaiseApplyMapper.insert(oaCRaiseApply);
        if (insert != 1){
            throw PangException.create(EErrorCode.applyAmountFailure);
        }
    }
}
